$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'station/list',
        datatype: "json",
        colModel: [			
			{ label: 'flowId', name: 'flowId', index: 'flow_id', width: 50, key: true },
			{ label: '气象站编号', name: 'stationId', index: 'station_id', width: 80 }, 			
			{ label: '记录时间', name: 'time', index: 'time', width: 80 }, 			
			{ label: '温度', name: 'temperature', index: 'temperature', width: 80 }, 			
			{ label: '最高气温', name: 'highTemp', index: 'high_temp', width: 80 }, 			
			{ label: '最低气温', name: 'lowTemp', index: 'low_temp', width: 80 }, 			
			{ label: '湿度', name: 'humidity', index: 'humidity', width: 80 }, 			
			{ label: '降水', name: 'rainfall', index: 'rainfall', width: 80 }, 			
			{ label: '风速', name: 'windSpeed', index: 'wind_speed', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		starttime:'',
        endtime:'',
        station_id:'',
		station: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		timequery: function (event) {
        			//input.focus();
        			//alert(vm.starttime);
        			 $("#jqGrid").jqGrid('setGridParam',{
        				 url:baseURL + 'station/timelist',
        				 postData:{
        					 'starttime':vm.starttime,
        					 'endtime':vm.endtime,
        					 'station_id':vm.station_id
        				 },
        				 datattype:'json',
        				 mtyp:'GET',
        				 viewrecords: true,
        			     height: 385,
        			     rowNum: 10,
        			     rowList : [10,30,50],
        			     rownumbers: true,
        			     rownumWidth: 25,
        			     autowidth:true,
        			     multiselect: true,
        			     pager: "#jqGridPager",
        			     jsonReader : {
        			         root: "page.list",
        			         page: "page.currPage",
        			         total: "page.totalPage",
        			         records: "page.totalCount"
        			     },
        			     prmNames : {
        			         page:"page",
        			         rows:"limit",
        			         order: "order"
        			     },
        			 }).trigger('reloadGrid');


        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.station = {};
		},
		update: function (event) {
			var flowId = getSelectedRow();
			if(flowId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(flowId)
		},
		saveOrUpdate: function (event) {
			var url = vm.station.flowId == null ? "station/save" : "station/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.station),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var flowIds = getSelectedRows();
			if(flowIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "station/delete",
                    contentType: "application/json",
				    data: JSON.stringify(flowIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(flowId){
			$.get(baseURL + "station/info/"+flowId, function(r){
                vm.station = r.station;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});